package com.atech.ebayadmin.merchandise.service.impl;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.atech.ebayadmin.common.pager.Pager;
import com.atech.ebayadmin.merchandise.dao.IMerchandiseDAO;
import com.atech.ebayadmin.merchandise.model.ModifyHistory;
import com.atech.ebayadmin.merchandise.model.Record;
import com.atech.ebayadmin.merchandise.service.IMerchandiseService;
import com.atech.ebayadmin.merchandise.vo.ModifyHistoryVO;
import com.atech.ebayadmin.merchandise.vo.RecordVO;
import com.atech.ebayadmin.system.service.ISystemService;
import com.atech.ebayadmin.system.vo.UserVO;

public class MerchandiseService implements IMerchandiseService{
	private final static org.apache.commons.logging.Log logger = 
		org.apache.commons.logging.LogFactory.getLog(MerchandiseService.class);
    private IMerchandiseDAO merchandiseDAO;
    private ISystemService systemService;

    /**
	 * @return the systemService
	 */
	public ISystemService getSystemService() {
		return systemService;
	}
	/**
	 * @param systemService the systemService to set
	 */
	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}
	/**
	 * @return the MerchandiseDAO
	 */
	public IMerchandiseDAO getMerchandiseDAO() {
		return merchandiseDAO;
	}
	/**
	 * @param MerchandiseDAO the MerchandiseDAO to set
	 */
	public void setMerchandiseDAO(IMerchandiseDAO merchandiseDAO) {
		this.merchandiseDAO = merchandiseDAO;
	}
    
	
	//Add for create record
	public Integer saveRecord(Record po)throws Exception{	
	    	Integer itemId = merchandiseDAO.saveRecord(po);
			return itemId;		
	    }
	// Add for list record
	public List<RecordVO> listRecord(Map vo, Pager pager) throws Exception{
		List<Record> poList = merchandiseDAO.listRecord(vo, pager);
		List<RecordVO> voList = new ArrayList<RecordVO>(); 
		System.err.println("==== return list size ====" + poList.size());
		for(Record po : poList){
			voList.add(poToVo(po));
		}
		return voList;
	}
	
	public RecordVO poToVo(Record po) throws Exception{
		
		NumberFormat integerFmt = new DecimalFormat("NNNNN");
		
		RecordVO re = new RecordVO();
 		re.setSequenceNum(String.valueOf(po.getSequenceNum()));
 		re.setAssetsNum(po.getAssetsNum());
 		re.setAssetsMgnt(po.getAssetsMgnt());
 		re.setBelongFrame(po.getBelongFrame());
 		re.setBusType(po.getBusType());
 		re.setChargeMan(po.getChargeMan());
 		re.setCompName(po.getCompName());
 		re.setCurrStatus(po.getCurrStatus());
 		re.setEquipType(po.getEquipType());
 		re.setEquipClass(po.getEquipClass());
 		re.setEquipFunction(po.getEquipFunction());
 		re.setEquipLocalCity(po.getEquipLocalCity());
 		re.setEquipLocalDistrict(po.getEquipLocalDistrict());
 		re.setEquipLocalProvice(po.getEquipLocalProvice());
 		re.setEquipLocalTown(po.getEquipLocalTown());
 		re.setEquipModel(po.getEquipModel());
 		re.setEquipName(po.getEquipName());
 		re.setEquipNickName(po.getEquipNickName());
 		re.setExactLocation(po.getExactLocation());
 		re.setFactoryName(po.getFactoryName());
 		re.setFiberEnd(po.getFiberEnd());
 		re.setFiberEndCode(po.getFiberEndCode());
 		re.setFiberEndCode(po.getFiberEndCode());
 		if (null != po.getFiberLength()) {
 			re.setFiberLength(String.valueOf(po.getFiberLength()));
 		}
 		if (null !=po.getFiberNum() ){
 			re.setFiberNum(String.valueOf(po.getFiberNum()));
 		}	
 		re.setFiberStart(po.getFiberStart());
 		re.setFiberType(po.getFiberType());
 		re.setInputDate(formteDate2Str(po.getInputDate()));
 		/*re.setIp(po.getIp());*/
 		re.setLocalChannel(po.getLocalChannel());
 		re.setLocalFrame(po.getLocalFrame());
 		re.setManDate(formteDate2Str(po.getManDate()));
 		re.setModelNum(po.getModelNum());
 		re.setModifyDate(formteDate2Str(po.getModifyDate()));
 		re.setModifyResult(po.getModifyResult());
 		re.setProdClass(po.getProdClass());
 		re.setProdType(po.getProdType());
 		re.setProjName(po.getProjName());
 		re.setProjNum(po.getProjNum());
 		re.setProvider(po.getProvider());
 		re.setSelfNum(po.getSelfNum());
 		re.setSn(po.getSn());
 		re.setMacAddr(po.getMacAddr());
 		re.setStandard(po.getStandard());
 		if (null != po.getUnitPrice()){
 			re.setUnitPrice(po.getUnitPrice().toString());
 		}
 		re.setUseDate(formteDate2Str(po.getUseDate()));
 		re.setUserId(po.getUserId());
 		re.setWareHouseMan(po.getWareHouseMan());
 		re.setFiberStartCode(po.getFiberStartCode());
 		re.setRemarks(po.getRemarks());
		return re;
	}
	private String formteDate2Str(Date date){
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != date) {
			str = sdf.format(date);
		}
		return str;
	}
	// Add for list record end

	// Add for save modify record history begin
	public Integer saveModHisty(UserVO user, Record reMd) {
		ModifyHistory modHisty = new ModifyHistory();
		modHisty.setSequenceNum(reMd.getSequenceNum());
		//UserVO user = (UserVO)super.getSession().get("User");//获取用户的会话对象
		modHisty.setAuthor(user.getUserName());
		modHisty.setModifyDate(reMd.getModifyDate());
		modHisty.setModifyReason(reMd.getModifyResult());
		Integer result = null;
		try {
			result = saveModHisty(modHisty);
		} catch (Exception e) {
			String message = (String) ServletActionContext.getRequest().getAttribute("message");
			message = message + "但无法写入修改历史!";
			ServletActionContext.getRequest().setAttribute("message", message);
			e.printStackTrace();
		}
		return result;
	}
	
	// Add for save modify history begin
		public Integer saveModHisty(ModifyHistory po)throws Exception{	
		    	Integer itemId = merchandiseDAO.saveModHisty(po);
				return itemId;		
		    }
	// Add for save modify history end
	
	// Add for retrieve modify history begin
	public List<ModifyHistoryVO> rtrvModifyHisty(Integer key) throws Exception{
		List<ModifyHistory> poList = merchandiseDAO.rtrvModifyHisty(key);
		List<ModifyHistoryVO> voList = new ArrayList<ModifyHistoryVO>(); 
		System.err.println("==== return list size ====" + poList.size());
		for(ModifyHistory po : poList){
			voList.add(mhPoToVo(po));
		}
		return voList;
	}
	
	public ModifyHistoryVO mhPoToVo(ModifyHistory po) throws Exception{
		ModifyHistoryVO reVO = new ModifyHistoryVO();
		reVO.setSequenceNum(String.valueOf(po.getSequenceNum()));
		reVO.setAuthor(po.getAuthor());
		reVO.setModifyReason(po.getModifyReason());
		reVO.setModifyDate(formteDate2Str(po.getModifyDate()));
		return reVO;
	}
	// Add for retrieve modify history end
	
	// Add for modify record begin
	// Add for modify record begin
	public Record getRecord(Integer id) throws Exception{
		Record po = merchandiseDAO.getRecord(id);
		return po;
	}
	
	public RecordVO getRecordVO(Integer id) throws Exception{
		Record po = merchandiseDAO.getRecord(id);
		return poToVo(po);
	}
	
	public void updateRecord(Record po)throws Exception{
		System.err.println("======= modify obje =====" + po);
		if (null == po) {
			System.err.println("=== modify po is null ===");
		} else {
			merchandiseDAO.updateRecord(po);
		}
    	
    	
    }
	// Add for modify record end
	
	// Add for delect select record begin
	public void deleteSlctRe(String[] slctRe, Map res)throws Exception{
		if (null == slctRe) {
			return;
		}
		List successList = new ArrayList();
		List failList = new ArrayList();
		for(String assetNumStr : slctRe) {
			if (StringUtils.isNotBlank(assetNumStr)) {
				Integer assetNum = Integer.valueOf(assetNumStr);
				try {
					Record re = merchandiseDAO.getRecord(assetNum);
					merchandiseDAO.deleteSlctRe(re);
					successList.add(assetNumStr);
				} catch (Exception e){
					failList.add(assetNumStr);
					e.printStackTrace();
				}
				try {
					merchandiseDAO.delSlctReModHisty(assetNum);
				} catch (Exception e){
					e.printStackTrace();
				} 	
			}
		}
		res.put("S", successList);
		res.put("F", failList);
	}
	// Add for delect select record end
}
	
